﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QuanLiSinhVien_v01.DTO;

namespace QuanLiSinhVien_v01.DAO
{
    class MonHocDao:DataProvider
    {
        public MonHocDto MonHoc { get; set; }

        protected override Object GetDataFromDataRow(DataTable dt, int rowIndex)
        {
            MonHocDto MonHocDto = new MonHocDto();
            MonHocDto.MaMonHoc = dt.Rows[rowIndex]["MaMonHoc"].ToString();
            MonHocDto.TenMonHoc = dt.Rows[rowIndex]["TenMonHoc"].ToString();
            MonHocDto.CreditNumber = Int32.Parse(dt.Rows[rowIndex]["CreditNumber"].ToString());
            MonHocDto.MaHocKy = dt.Rows[rowIndex]["MaHocKy"].ToString();
            MonHocDto.GhiChu = dt.Rows[rowIndex]["GhiChu"].ToString();

            return MonHocDto;
        }

        public MonHocDto[] GetList()
        {
            string strCommand = " Select * from MonHoc ";
            DataTable dt = ExcuteQuery(strCommand);
            int n = dt.Rows.Count;
            MonHocDto[] kq = new MonHocDto[n];
            for (int i = 0; i < n; i++)
            {
                kq[i] = (MonHocDto)GetDataFromDataRow(dt, i);
            }
            return kq;
        }

        public void Insert()
        {
            string strCommand =
                "insert into MonHoc (MaMonHoc, TenMonHoc, CreditNumber, MaHocKy , GhiChu) " +
                "values (N'" + MonHoc.MaMonHoc + "', N'" +
                              MonHoc.TenMonHoc + "', " +
                              MonHoc.CreditNumber + ", N'" +
                              MonHoc.MaHocKy + "', N'" +
                              MonHoc.GhiChu + "'" +
                        ")";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool CheckExist(string MaMonHoc)
        {
            string strCommand = " Select * from MonHoc " +
                                "where MaMonHoc = N'" + MaMonHoc + "'";
            DataTable dt = ExcuteQuery(strCommand); 
            if (dt.Rows.Count != 0)
                return true;
            return false;
        }
        public void Update()
        {
            string strCommand =
                "update MonHoc set " +
                "TenMonHoc = N'" + MonHoc.TenMonHoc + "' , " +
                "CreditNumber = " + MonHoc.CreditNumber + " , " +
                "MaHocKy = N'" + MonHoc.MaHocKy + "' , " +
                "GhiChu = N'" + MonHoc.GhiChu + "' " +
                "where MaMonHoc = '" + MonHoc.MaMonHoc + "' ";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete()
        {
            string strCommand =
                "DELETE FROM MonHoc " +
                "WHERE  MaMonHoc = '" + MonHoc.MaMonHoc + "'";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public MonHocDto[] Search(string keywork)
        {
            string strCommand = " Select * from MonHoc " +
                                "where MaMonHoc like '%" + keywork + "%' " +
                                " or TenMonHoc like '%" + keywork + "%' " +
                                " or MaHocKy like '%" + keywork + "%' " +
                                " or CreditNumber like '%" + keywork + "%' " +
                                 "or GhiChu like '%" + keywork + "%' ";
            DataTable dt = ExcuteQuery(strCommand);
            int n = dt.Rows.Count;
            MonHocDto[] kq = new MonHocDto[n];
            for (int i = 0; i < n; i++)
            {
                kq[i] = (MonHocDto)GetDataFromDataRow(dt, i);
            }
            return kq;
        }

    }
}
